<template>
  <el-form
    :model="userForm"
    label-width="100px"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model="userForm.username"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="userForm.age"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="userForm.password" show-password></el-input>
    </el-form-item>
    <el-form-item label="生日" required>
      <el-col :span="11">
        <el-form-item prop="birthday">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="userForm.birthday"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="userForm.sex">
        <el-radio label="1">男</el-radio>
        <el-radio label="2">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('userForm')"
        >立即创建</el-button>
      <el-button @click="resetForm('userForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      userForm: {
        id: "",
        username: "高达",
        password: "123456",
        age: "22",
        birthday: "2021-04-04",
        sex: "1",
      },
    };
  }, 
  mounted:function(){
    var app=this;
    if (this.$route.params.id) {
       app.$httpRequest
        .get("user/getById/" + app.$route.params.id, {})
        .then(function ({data}) {
          console.log("查询id");
          console.log(data);
          app.userForm=data.data;
          app.userForm.sex=app.userForm.sex+"";
        })
        .catch(function (error) {
          //失败后调用
        })
        .then(function () {
          //不论成功或者失败后都调用
        });
    }
  },
  methods: {
    submitForm(formName) {
      console.log(this.$route.params.id);
      if (this.$route.params.id) {
        this.userForm.id = this.$route.params.id;
        var app = this;
        this.$httpRequest
          .post("user/update", this.userForm)
          .then(function ({ data }) {
            if (data.code == 200) {
              console.log("修改成功");
              app.$message({
                showClose: true,
                message: "恭喜你,修改成功",
                type: "success",
              });
              app.$router.push("/toUserList");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      } else {
        var app = this;
        this.$httpRequest
          .post("user/insert", this.userForm)
          .then(function ({ data }) {
            if (data.code == 200) {
              app.$message({
                showClose: true,
                message: "恭喜你，添加成功",
                type: "success",
              });
              console.log("添加成功");
              app.$router.push("/toUserList");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toIndex() {
      console.log("进来了");
      this.$router.push("/toUserList");
    },
  },
};
</script>

<style>
</style>